package com.fenqing.leetcode.code.t0_200.t0_100;

import com.fenqing.annotations.Execute;
import com.fenqing.core.Topic;

import java.util.Stack;

/**
 * @author fenqing
 */
public class Topic020 extends Topic {
    /**
     * 构造器
     *
     * @param input 输入
     */
    public Topic020(String input) {
        super(input);
    }


    @Execute
    public boolean isValid(String s) {
        Stack<Character> characters = new Stack<>();
        char[] chars = s.toCharArray();

        for (char aChar : chars) {
            if (aChar == '}') {
                if (characters.isEmpty() || characters.peek() != '{') {
                    return false;
                } else {
                    characters.pop();
                }
            } else if (aChar == ']') {
                if (characters.isEmpty() || characters.peek() != '[') {
                    return false;
                } else {
                    characters.pop();
                }
            } else if (aChar == ')') {
                if (characters.isEmpty() || characters.peek() != '(') {
                    return false;
                } else {
                    characters.pop();
                }
            } else {
                characters.push(aChar);
            }
        }
        return characters.isEmpty();
    }

    public static void main(String[] args) {
        boolean run = new Topic020("()").run();
        System.out.println(run);
    }
}
